#====# Replicates results in the main text #====#

# Setup ----
library(bizdays)
library(ggpubr)
library(lemon)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/event_function.R")
source("aux/plot_theme.R")

# prepare business calendar:
business_calendar <- create.calendar('biz_calendar', weekdays = c('saturday','sunday'))

# Import data ----
stocks <- read_rds("data_out/stocks_analysis.rds") 

length(unique(stocks$ticker_symbol)) # 497 firms
length(unique(stocks$ticker_symbol[stocks$FCPA_sample == 1])) # 261 past FCPA target
length(unique(stocks$ticker_symbol[stocks$FCPA_sample == 0])) # 236 matched placebos

# Figure 2 ----
stocks %>%
  filter(date <= as.Date("2025-02-14")) %>%
  filter(FCPA_sample == 1) %>%
  group_by(date, FCPA_sample) %>%
  reframe(obs = mean(obs_chg, na.rm = TRUE),
          fit = mean(fit_chg, na.rm = TRUE)) %>%
  filter(!is.na(fit)) %>%
  pivot_longer(cols = -c("date", "FCPA_sample")) %>%
  mutate(FCPA_sample = fct_relevel(as.factor(FCPA_sample), "1")) %>%
  ggplot(aes(x = date, y = value)) +
  annotate(geom = "rect",
           xmin = offset(unique(stocks$event), n = -30, business_calendar),
           xmax = offset(unique(stocks$event), n = -5, business_calendar),
           ymin = -Inf, ymax = +Inf, alpha = .5, fill = "grey90") +
  geom_bracket(data = data.frame(xmin = offset(unique(stocks$event), n = -30, business_calendar),
                                 xmax = offset(unique(stocks$event), n = -5, business_calendar),
                                 y.position = c(2)),
               mapping = aes(xmin = xmin,
                             xmax = xmax,
                             y.position = y.position),
               label = "Estimation window",
               label.size = 3.5,
               bracket.shorten = 0.5) +
  geom_label(data = data.frame(x = as.Date("2025-02-06"), 
                               y = c(2, 2),
                               FCPA_sample = c(1, 0)),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-02-08"), 
                               xend = as.Date("2025-02-10")-0.6,
                               y = 2.1, 
                               yend = 2),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  geom_line(aes(linetype = name)) +
  geom_point(aes(shape = name), fill = "white", size = 2.5) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.3, linewidth = 0.1) +
  xlab("") + ylab("Average returns") +
  scale_linetype_manual("", breaks = c("obs", "fit"), 
                        labels = c("Observed returns", "Counterfactual returns"),
                        values = c("solid", "dotted")) +
  scale_shape_manual("", breaks = c("obs", "fit"), 
                     labels = c("Observed returns", "Counterfactual returns"),
                     values = c(19, 21)) +
  scale_y_continuous(labels = ~paste0(.x, "%")) +
  coord_cartesian(ylim = c(-2.5, 2.5)) +
  theme(legend.key.width = unit(2, "cm"),
        legend.box.margin = margin(-25, 0, 0, 0))
ggsave("plots/figure_2.pdf", width = 9, height = 3)

# Figure 3 ----
est <- return_daily_avg(data = stocks, abn_ret = "abn_chg", cum_abn_ret = "car", stratum = "FCPA_sample")

est %>%
  filter(FCPA_sample != "diff") %>%
  mutate(FCPA_sample = fct_relevel(FCPA_sample, "1")) %>%
  ggplot(aes(x = date, y = estimate)) +
  geom_line(aes(linetype = dv), position = position_dodge2(width = 0.5),
            show.legend = FALSE) +
  geom_pointrange(aes(ymin = ci.lo, ymax = ci.hi, shape = dv),
                  position = position_dodge2(width = 0.5),
                  fill = "grey") +
  geom_label(data = data.frame(x = as.Date("2025-02-07")+0.5, 
                               y = c(1, 1),
                               dv = c("ar", "ar"),
                               FCPA_sample = c(1, 0)),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-02-08"), 
                               xend = as.Date("2025-02-10")-0.6,
                               y = c(1.1, 1.1), 
                               yend = c(0.9, 0.9),
                               dv = c("ar", "ar"),
                               FCPA_sample = c(1, 0)),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  facet_rep_wrap(c("FCPA_sample"), nrow = 2, repeat.tick.labels = "all",
                 scales = "free_x",
                 labeller = as_labeller(c("0" = "Non-FCPA targets",
                                          "1" = "Past FCPA targets",
                                          "ar" = "Abnormal Returns",
                                          "car" = "Cumulative Abnormal Returns"))) +
  xlab("") + ylab("Average returns") +
  scale_linetype_manual("", labels = c("Abnormal Returns", "Cumulative Abnormal Returns"),
                        values = c("longdash", "dashed")) +
  scale_shape_manual("", labels = c("Abnormal Returns", "Cumulative Abnormal Returns"),
                     values = c(21, 4)) +
  scale_y_continuous(labels = ~paste0(.x, "%")) +
  # enlarge legend keys:
  theme(legend.key.width = unit(2, "cm"),
        strip.clip = "off", 
        legend.box.margin = margin(-25, 0, 0, 0))
ggsave("plots/figure_3.pdf", width = 7, height = 4)

# Figure 4 ----
est <- return_daily_avg(data = stocks %>%
                          filter(FCPA_sample == 1), 
                        abn_ret = "abn_chg", cum_abn_ret = "car", stratum = "ongoing")

est %>%
  filter(ongoing != "diff") %>%
  ggplot(aes(x = date, y = estimate)) +
  geom_line(aes(linetype = dv), position = position_dodge2(width = 0.5),
            show.legend = FALSE) +
  geom_pointrange(aes(ymin = ci.lo, ymax = ci.hi, shape = dv),
                  position = position_dodge2(width = 0.5),
                  fill = "grey") +
  geom_label(data = data.frame(x = as.Date("2025-02-07")+0.5, 
                               y = c(5, 5),
                               ongoing = c(1, 0),
                               dv = "ar"),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-02-08"), 
                               xend = as.Date("2025-02-10")-0.6,
                               y = c(5.1, 5.1), 
                               yend = c(4, 4),
                               dv = c("ar", "ar"),
                               ongoing = c(1, 0)),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  xlab("") + ylab("Average returns") +
  scale_linetype_manual("", labels = c("Abnormal Returns", "Cumulative Abnormal Returns"),
                        values = c("longdash", "dashed")) +
  scale_shape_manual("", labels = c("Abnormal Returns", "Cumulative Abnormal Returns"),
                     values = c(21, 4)) +
  scale_y_continuous(labels = ~paste0(.x, "%")) +
  facet_rep_wrap(c("ongoing"), nrow = 2, repeat.tick.labels = "all",
                 labeller = as_labeller(c("0" = "Non-ongoing investigations",
                                          "1" = "Ongoing investigations"))) +
  # enlarge legend keys:
  theme(legend.key.width = unit(2, "cm"),
        strip.clip = "off", 
        legend.box.margin = margin(-25, 0, 0, 0))
ggsave("plots/figure_4.pdf", width = 7, height = 4)

#====# The End #====#